package com.thirtydays.frame.module.home.view

import android.bluetooth.BluetoothGatt
import android.content.Intent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.NetworkUtils
import com.thirtydays.frame.R
import com.thirtydays.frame.base.BaseFragment
import com.thirtydays.frame.constant.NOTIFY_UUID
import com.thirtydays.frame.constant.SERVICE_UUID
import com.thirtydays.frame.constant.WRITE2_UUID
import com.thirtydays.frame.constant.WRITE4_UUID
import com.thirtydays.frame.data.DataManager
import com.thirtydays.frame.data.bean.DeviceData
import com.thirtydays.frame.databinding.HomeFragmentBinding
import com.thirtydays.frame.module.home.adapter.DeviceAdapter
import com.thirtydays.frame.module.home.model.HomeViewModel
import com.thirtydays.frame.util.BluetoothHelp
import com.thirtydays.frame.util.ext.clicks
import com.xm.bluetoothkotlin.BluetoothManager
import com.xm.bluetoothkotlin.connect.BleBluetooth
import com.xm.bluetoothkotlin.connect.ConnectConfigData
import com.xm.bluetoothkotlin.connect.back.BluetoothConnectCallback
import com.xm.bluetoothkotlin.device.BluetoothDeviceData
import com.xm.bluetoothkotlin.receiver.ReceiverCallBackImp
import com.xm.bluetoothkotlin.search.BluetoothSeachCallback
import com.xm.bluetoothkotlin.search.BluetoothSeachCallbackImp
import com.xm.bluetoothkotlin.search.BluetoothSearchFilter
import java.util.*

class HomeFragment : BaseFragment<HomeViewModel, HomeFragmentBinding>() {
    companion object {
        fun newInstance() = HomeFragment()
    }

    private val mAdapter by lazy { DeviceAdapter() }
    override fun init() {
        with(mViewBinding) {
            setRvAdapter(dataRv, LinearLayoutManager(requireContext()), mAdapter)
        }
    }

    override fun initObserve() {
        with(mViewModel) {

        }
    }

    override fun initListener() {
        with(mViewBinding) {
            open.clicks {
                BluetoothManager.enableBluetooth()
            }

            scan.clicks {
                val filter = BluetoothSearchFilter().apply {
                    UUIDs = mutableListOf(UUID.fromString(SERVICE_UUID))
                }

                BluetoothManager.search()
            }

            BluetoothManager.addSearchCallback(object : BluetoothSeachCallbackImp() {
                override fun onSearchSuccess(devices: MutableList<BluetoothDeviceData>, newDevice: BluetoothDeviceData) {
                    LogUtils.e(
                        "HomeFragment",
                        "device:${newDevice.device} address:${newDevice.address} name:${newDevice.name} rssi:${newDevice.rssi}")
                    mAdapter.addData(DeviceData(newDevice.name, newDevice.address))
                }

                override fun onStartSearch() {
                    LogUtils.e("搜索开始")
                }

                override fun onStopSearch() {
                    LogUtils.e("搜索停止")
                }
            })

            mAdapter.addChildClickViewIds(R.id.linkAtv)
            mAdapter.setOnItemChildClickListener { _, view, position ->
                val oldItem = mAdapter.getItem(position)
                when (view.id) {
                    R.id.linkAtv -> {
                        val filter = BluetoothManager.connectDevices.keys.filter { it == oldItem.deviceMac }
                        if (filter.isEmpty()) {
                            BluetoothHelp.connect(oldItem.deviceMac)
                        } else {
                            BluetoothManager.disconnect(oldItem.deviceMac)
                        }
                    }
                }
            }

        }
    }
}